
package kaizone.haining.chaobohui.fragment;

import java.util.List;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.activity.GuideActivity;
import kaizone.haining.chaobohui.json.AboutDO;
import kaizone.haining.chaobohui.json.AppRecommend;
import kaizone.haining.chaobohui.net.DataControl;
import kaizone.haining.chaobohui.widget.CBHItemDropView;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class CBHAboutFragment extends Fragment implements DataControl.IDataUpdateListener,
        CBHItemDropView.OnDropClickListener {

    public static final String DATA_SOURCE_URL = "http://api.dp-app.com/chao/remote/rest/about.htm";

    private FragmentManager mFragmentManager;

    private CBHItemDropView mCbhAppInfoView;

    private CBHItemDropView mRecommendAppView;

    private CBHItemDropView mAboutView;

    private CBHItemDropView mIllustrateView;

    private DataControl mDataControl;

    public CBHAboutFragment(FragmentManager mgr) {
        mFragmentManager = mgr;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = (View) inflater.inflate(R.layout.cbh_fragment_about, null);
        mCbhAppInfoView = (CBHItemDropView) view.findViewById(R.id.view01);
        mRecommendAppView = (CBHItemDropView) view.findViewById(R.id.view02);
        mAboutView = (CBHItemDropView) view.findViewById(R.id.view03);
        mIllustrateView = (CBHItemDropView) view.findViewById(R.id.view04);
        initwidget();

        mDataControl = new DataControl(getActivity());
        mDataControl.addChangeListener(this);

        return view;
    }

    private void initwidget() {
        mCbhAppInfoView.setFlagText("潮博会APP介绍");
        mCbhAppInfoView.setFlagImage(R.drawable.png_about1);
        mCbhAppInfoView.setFragmentManager(mFragmentManager);
        mCbhAppInfoView.setContentText(getString(R.string.app_js));

        mRecommendAppView.setFlagText("推荐应用");
        mRecommendAppView.setFlagImage(R.drawable.png_otherapp1);
        mRecommendAppView.setFragmentManager(mFragmentManager);

        mAboutView.setFlagText("关于编者");
        mAboutView.setFlagImage(R.drawable.png_aboutus1);
        mAboutView.setFragmentManager(mFragmentManager);
        mAboutView.setContentText(getString(R.string.gybz));

        mIllustrateView.setFlagText("演示使用方法");
        mIllustrateView.setFlagImage(R.drawable.png_caozuo1);
        mIllustrateView.setFragmentManager(mFragmentManager);

        mCbhAppInfoView.setDropClickListener(this);
        mRecommendAppView.setDropClickListener(this);
        mAboutView.setDropClickListener(this);
        mIllustrateView.setDropClickListener(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        mDataControl.exec(DATA_SOURCE_URL);
    }

    public void hideAllItem() {
        mCbhAppInfoView.hideDropContent();
        mRecommendAppView.hideDropContent();
        mAboutView.hideDropContent();
        mIllustrateView.hideDropContent();
    }

    @Override
    public Object backParse(String datastring, String requeststring) {
        if (TextUtils.isEmpty(datastring))
            return null;
        Object obj = null;
        if (requeststring.equals(DATA_SOURCE_URL)) {
            AboutDO aboutDO = AboutDO.parseJson(datastring);
            obj = aboutDO;
        }
        return obj;
    }

    @Override
    public void foreUpdate(Object result, String requeststring) {
        if (result instanceof AboutDO) {
            AboutDO aboutDO = (AboutDO) result;
            List<AppRecommend> data = aboutDO.getData();
            mRecommendAppView.setAppRecommends(data);
        }
    }

    @Override
    public void exception(Exception e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStartOnClick(View v) {
        if (v == mIllustrateView.getTitlePanel()) {
            GuideActivity.come(getActivity(), 1);
        }
    }

    @Override
    public void onBeforeDropContentExtend(View v) {
        hideAllItem();

    }

    @Override
    public void onAfterDropContentExtend(View v) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onBeforeDropContentHide(View v) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAfterDropContentHide(View v) {
        // TODO Auto-generated method stub

    }
}
